package org.opensuse.bs.ui.wizards;

import java.util.HashMap;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.opensuse.bs.ui.BSUIPlugin;

public class ConnectionWizard extends Wizard implements INewWizard{
	private BSPage bpage;
	
	private HashMap<String, HashMap<String, String>> sections;
	
	private void openPerspective() {
		try {
			IWorkbenchWindow window = 
				PlatformUI.getWorkbench().getActiveWorkbenchWindow();
			PlatformUI.getWorkbench().showPerspective(
					"org.opensuse.bs.ui.BSPerspective", window);
		}catch(WorkbenchException e) {
			System.out.println(e.getMessage());
		}
	}
	
	public ConnectionWizard() {
		super();
		setWindowTitle("Build Service Project");
		sections = BSUIPlugin.getDefault().getSections();
		setNeedsProgressMonitor(true);
	}

	public void addPages() {
		bpage = new BSPage(sections);
		addPage(bpage);
	}
	
	public boolean performFinish() {
		String url = bpage.doFinish();
		BSUIPlugin.getDefault().setApiurl(url);
		HashMap<String, String> section = sections.get(url);
		BSUIPlugin.getDefault().setUser(section.get("user"));
		openPerspective();
		return true;
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) {
	}
	
}
